package com.heima.wemedia;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.IOException;

/**
 * @author mrchen
 * @date 2022/7/11 14:45
 */
public class JsoupDemo {


    @Test
    public void jsoup() throws IOException {
        System.setProperty("webdriver.chrome.driver", "C:\\worksoft\\tools\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://3g.163.com/touch/ent?ver=c&clickfrom=index2018_ent_bottom");
        Document document = Jsoup.parse(driver.getPageSource());

//        Document document = Jsoup.connect("https://3g.163.com/touch/ent?ver=c&clickfrom=index2018_ent_bottom")
//                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36")
//                .get();
        Elements elementList = document.getElementsByTag("article");
        for (Element element : elementList) {
            Element titleEle = element.getElementsByClass("title").get(0);
            System.out.println(titleEle.text());
            Element newsPicEle = element.getElementsByClass("news-pic").get(0);
            Elements imgList = newsPicEle.getElementsByTag("img");
            for (Element img : imgList) {
                String url = img.attr("data-src");
                System.out.println(url);
            }
        }
    }
}
